function extend(obj, source){
    for (var item in source){
        obj[item] = source[item];
    }
    return obj
};
(function (win, doc) {
    win.$ = function (id) {return doc.getElementById(id)};

    win.$$ = {
        setCookie: function (name, value) {
            var Days = 30;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days*24*60*60*1000);
            doc.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        },
        getCookie: function getCookie(name){
            var arr, reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
            if (arr = doc.cookie.match(reg)) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        delCookie: function (name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval=this.getCookie(name);
            if(cval!=null){
                doc.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
            }
        },
        ajax:function(configs){
            var options={
                'type': configs.type,
                'url' : configs.url,
                'data': configs.data,
                'success':null,
                'error' : null
            };
            var opts = extend(options, configs);
            var obj=new XMLHttpRequest();  // XMLHttpRequest对象用于在后台与服务器交换数据
            obj.open(options.type || 'GET', options.url, true);
            if(options.type == 'POST' || options.type == 'post'){
                obj.setRequestHeader("Content-type", "application/json"); // 发送信息至服务器时内容编码类型
                obj.send(JSON.stringify(opts.data));
            }else{
                obj.send(null);
            }
            obj.onreadystatechange = function () {
                if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) {  // 304未修改
                    opts.success.call(this, obj.responseText);
                }
            };
        }
    }

    setTimeout(function(){
        $('wrapper').className='loaded';
    },100);

    win.kblog = {
        postArticle: function (configs) {
            var options = {
                html:null
            };
            var opts = extend(options, configs);
            $('savePost').onclick=function(){
                var html= opts.html ? UE.getEditor('ueditor').getContent() : $('preview').innerHTML,
                    articleid=$('articleid').value,
                    title=$('title').value,
                    cates=$('cates').value,
                    date=$('date').value,
                    author=$('author').value,
                    update=$('update').checked,
                    imgid=$('imgid').value;
                if(html==''){
                    alert('文章内容不能为空');
                    return;
                }
                if(articleid==''){
                    alert('文章 id 不能为空');
                    return;
                }
                $$.ajax({
                    type : 'POST',
                    url:'/postArticle',
                    data:{
                        'innerHTML': html,
                        'articleid':articleid,
                        'title':title,
                        'cates':cates,
                        'date' : date,
                        'author':author,
                        'update':update,
                        'imgid' :imgid
                    },
                    success:function(data){
                        var _data=JSON.parse(data);
                        if(_data.status==1){
                            alert(_data.msg);
                            window.location.reload();
                        }else{
                            alert(_data.msg);
                        }
                    }
                });
            };
        }
    }
})(window, document);

    